package cn.me.authorization.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
 * spring-security配置类
 * @author zchcpywin10
 */
@Configuration
@EnableWebSecurity
@Slf4j
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
  }
  @Override
  public void configure(WebSecurity web) {
    super.configure(web);
  }
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //配置基于内存的登录用户
    auth
      //
      .inMemoryAuthentication()
      //
      .passwordEncoder(passwordEncoder())
      //
      .withUser("user")
      //
      .password(passwordEncoder().encode("123456"))
      //
      .roles("USER");
  }
  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }
  /**
   * 认证管理
   *
   * @return 认证管理对象
   * @throws Exception 认证异常信息
   * // @Bean 父类并没有他注册为一个bean，所以要手动注册
   */
  @Override
  @Bean
  public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
  }
}
